﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HitSplat : MonoBehaviour 
{

	public Image image;
	public Sprite redSprite;
	public Sprite blueSprite;
	public int damage;
	public float radiusOfAppearance;
	public float duration;
	public Transform affected;
	public Text text;
	private Vector3 offset;
	private RectTransform rt;
	private float endTime;
	void Awake () 
	{
		float theta = Random.Range(0,2*Mathf.PI);
		float r = Random.Range(0,radiusOfAppearance);
		offset = new Vector3(r*Mathf.Cos(theta),r*Mathf.Sin(theta),0);
		rt = GetComponent<RectTransform>();
		endTime = Time.time + duration;
	}
	
	void Update () 
	{
		
		if (Time.time > endTime || affected == null)
		{
			GameObject.Destroy(this.transform.gameObject);
		} else
		{
			if (Camera.main != null)
			{
				rt.position = Camera.main.WorldToScreenPoint(affected.transform.position) + offset;
			}
		}
	}
	public void SetDamage(int damage)
	{
		if (damage>0)
		{
			image.sprite = redSprite;
		} else
		{
			image.sprite = blueSprite;
		}
		text.text = damage.ToString();
	}
}
